package com.huatai.datadevelop.service;

import com.alibaba.fastjson.JSON;
import com.huatai.datadevelop.entity.RealApiManageEntity;
import com.huatai.datadevelop.entity.RealApiVonversionEntity;
import com.huatai.datadevelop.utils.RedisCache;
import com.huatai.datadevelop.utils.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InitRedisService {

    @Autowired
    private RealApiVonversionService realApiVonversionService;
    @Autowired
    private RealApiManageService realApiManageService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动的时同步redis缓存的数据
     */
    @PostConstruct
    public void init(){
        List<RealApiVonversionEntity> vonversionEntityList = this.realApiVonversionService.list();
        List<RealApiManageEntity> manageEntityList = this.realApiManageService.list();
        if(manageEntityList!=null && manageEntityList.size()>0 && vonversionEntityList!=null && vonversionEntityList.size()>0){
            manageEntityList.stream().forEach(realApiManageEntity -> {
                List<RealApiVonversionEntity> realApiVonversionEntities = vonversionEntityList.stream().filter(p -> p.getRealapiId().equalsIgnoreCase(realApiManageEntity.getId().toString())).collect(Collectors.toList());
                if(realApiVonversionEntities!=null && realApiVonversionEntities.size()>0){
                    this.redisCache.set(RedisKeys.getRealApiConversionKey(realApiManageEntity.getId().toString()), JSON.toJSONString(realApiVonversionEntities),-1L);
                }
            });
        }
    }
}
